import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export const store = new Vuex.Store({
  state: {
    isVip: false, // 是否为VIP
    totalNumber: 155,
    // id: '75536f97cdeed3cdf6a53dd4470b07c9', // 用户uid 75536f97cdeed3cdf6a53dd4470b07c9
    id: '', // 用户uid
    result: [], // 已选项
    reportSrc: '', // 优学报告src
    slide: true, // subject组件(题目)显示与否
    showDialog: false, // 是否显示模态框
    questions: [], // 所有题目
    transitionName: 'slide-left', // 题目过渡动画
    count: 1, // 已答至第几题
    pageDirection: 'fade', // 根路由切换动画
    routeChain: [], // 根路由切换动画相关
    isTest: false, // 判断是否测试过
    dialog: { // 模态框提示信息
      cancel: '取消', // 取消按钮
      confirm: '确定', // 确定按钮
      content: '如果放弃，下次需要重新开始测评', // 提示内容
      callback: null, // 回调
      args: null // callback的参数
    }
  },
  mutations: {
    setIsVip (state, boolean) {
      state.isVip = boolean
    },
    setUID (state, uid) { // 缓存UID
      state.id = uid
    },
    setIsTest (state, boolean) { // 是否已测试
      state.isTest = boolean
    },
    setAnswer (state, answer) { // 缓存试题
      state.questions = answer
    },
    setResult (state, result) { // 缓存已选项
      // if (state.result.length === 155) return
      if (~result) {
        if (state.result.length === 155) return
        state.result.push(result)
      } else {
        state.result.pop()
      }
      // ~result ? state.result.push(result) : state.result.pop()
    },
    clearResult (state) { // 缓存已选项
      state.result.length = 0
    },
    toggleSlide (state) { // slide取反
      state.slide = !state.slide
    },
    toggleDialog (state, boolean) { // showDialog取反
      state.showDialog = !state.showDialog
    },
    setReportSrc (state, src) { // 设置获取优学报告链接
      state.reportSrc = src
    },
    setCount (state) { // 清除
      state.count = 0
    },
    setDialogContent (
      state,
      {
        cancel,
        content,
        confirm,
        args = null,
        callback = null
      }
    ) { // 设置模态框显示内容
      state.dialog = {
        args,
        cancel,
        confirm,
        content,
        callback
      }
      this.commit('toggleDialog')
    },
    setTransition (state, direction) { // 设置subject组件过渡动画
      state.transitionName = direction
    },
    addRouteChain (state, route) { // 设置根路由过渡动画
      state.routeChain.push(route)
    },
    popRouteChain (state) { // 设置根路由过渡动画
      state.routeChain.pop()
    },
    setPageDirection (state, dir) { // 设置根路由过渡动画
      state.pageDirection = dir
    }
  }
})
